﻿/// <reference path="../release/ajaxstack-core.js"/>
/// <reference path="../release/jsApi-vsdoc.js"/>

function FormExt()
{
}
var F = FormExt;
F.eachField = function(fieldSels, validateFn)
{
	if (is.String(fieldSels)) fieldSels = [fieldSels];

	var gValid = true;
	A.each(fieldSels, function(field)
	{
		var jq = $(field);
		jq.each(function(i)
		{
			var valid = validateFn($(this));
			gValid = gValid && valid;
		});
	});

	return gValid;
}
F.validateRequired = function(fieldSels)
{
	return F.eachField(fieldSels, function(jq)
	{
		var val = S.trim(jq.val());
		if (!val)
		{
			JQ.addAfter(jq, { 'class': 'error' }, "* required");
			return false;
		}
		return true;
	});
}
F.validateEmail = function(fieldSels)
{
	return F.eachField(fieldSels, function(jq)
	{
		var val = S.trim(jq.val());
		if (!val)
		{
			JQ.addAfter(jq, { 'class': 'error' }, "* required");
			return false;
		}

		var regEx = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if (regEx.test(val) == false)
		{
			JQ.addAfter(jq, { 'class': 'error' }, "* invalid email");
			return false;
		}

		return true;
	});
}
F.validateTextDate = function(fieldSels)
{
	return F.eachField(fieldSels, function(jq)
	{
		var inputs = jq.children('INPUT');
		if (inputs.length != 3)
		{
			JQ.addAfter(jq, { 'class': 'error' }, "* invalid control");
			return false;
		}

		if (F.parseTextDate(jq) == null)
		{
			JQ.addAfter(jq, { 'class': 'error' }, "* invalid date");
			return false;
		}

		return true;
	});
}
F.parseTextDate = function(jq)
{
	var inputs = jq.children('INPUT');
	
	var dd = $(inputs.get(0)).val(),
		mm = $(inputs.get(1)).val(),
		yyyy = $(inputs.get(2)).val();

	if (!is.IntegerInRange(parseInt(dd, 10), 1, 31)
			|| !is.IntegerInRange(parseInt(mm, 10), 1, 12)
			|| !is.IntegerInRange(parseInt(yyyy, 10), 1000, 9999))
	{
		return null;
	}

	return new Date(yyyy, mm, dd);
}
F.validate = function(formIdSel)
{
	$(formIdSel + " .error").remove();
	
	var valid = true;
	if (!F.validateRequired(formIdSel + " .required")) valid = false;
	if (!F.validateEmail(formIdSel + " .required-email")) valid = false;
	if (!F.validateTextDate(formIdSel + " .required-textdate")) valid = false;
	return valid;
}
F.bindResponseError = function(formIdSel, responseError)
{
	var fieldErrorMap = responseError.getFieldErrorMap();

	var boundAtLeastOne = false;
	$(formIdSel + " :input").each(function(i)
	{
		var fieldName = $(this).attr('name');
		var fieldError = fieldErrorMap[fieldName];
		if (fieldError)
		{
			boundAtLeastOne = true;
			JQ.addAfter($(this), { 'class': 'error' }, "* " + fieldError.errorMessage);
		}
	});

	if (!boundAtLeastOne)
	{
		$(formIdSel + ' .message').html(H.span({ 'class': 'error' }, responseError.errorMessage));
	}

	return boundAtLeastOne;
}
F.bindForm = function(formIdSel, options)
{
	options = options || {};

	if (options.submitFn)
	{
		if (options.submitButton)
		{
			$(options.submitButton).click(options.submitFn);
		}

		$(formIdSel + " INPUT").each(function(i)
		{
			var fieldType = $(this).attr('type');
			if (fieldType.toLowerCase() == "text" || fieldType.toLowerCase() == "password")
			{
				$(this).keypress(function(e)
				{
					if (Key.isEnterKey(e.which))
					{
						if (options.submitButton) 
							$(options.submitButton).click();
						else 
							options.submitFn();
					}
				});
			}
		});
	}
}


